package xyz.ljcc.ccmt.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import xyz.ljcc.ccmt.model.base.User;

/**
 * 登录拦截器
 * @author liucan
 *
 */
public class LoginInterceptor implements HandlerInterceptor{

	Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		logger.info(request.getRequestURI());
	 	HttpSession session = request.getSession();
	 	User user = (User) session.getAttribute("user");
//	 	Object user =  session.getAttribute("user");
		if(user == null) {
			logger.info(request.getRequestURL() + "页面跳转");
			response.sendRedirect(request.getContextPath() + "/login.html");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
	
	
	
}
